package com.hustcad.plm.pdm.file.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.StopWatch;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.util.IOUtils;
import com.hustcad.plm.basic.util.TyplmEntityBaseUtil;

import com.hustcad.plm.pdm.file.service.TyFileHandleService;
import com.hustcad.plm.pdm.file.service.TyplmFileHandleService;

import com.hustcad.plm.pdm.file.service.TyplmS3ClientAdapterService;
import com.hustcad.plm.pdm.file.util.TyplmExcelUtil;
import com.hustcad.plm.pdm.file.util.TyplmFileCopyRuleUtils;
import com.hustcad.plm.pdm.file.util.TyplmS3ClientUtil;
import com.hustcad.plm.pdm.file.util.TyplmUploadToolUtil;
import com.hustcad.plm.pdm.file.util.TyplmZipFileToolUtil;
import com.hustcad.plm.pdm.file.util.UploadFileUtils;
import com.hustcad.plm.pdm.filevault.model.vo.ContentDataFileVO;
import com.hustcad.plm.pdm.filevault.model.vo.ContentReferenceVO;
import com.hustcad.plm.pdm.filevault.model.vo.ContentUploadVO;
import com.hustcad.plm.pdm.filevault.model.vo.ContentUploadVaultVO;
import com.hustcad.plm.pdm.filevault.model.vo.FileDataVO;
import com.hustcad.plm.pdm.filevault.model.vo.FileServerVO;
import com.hustcad.plm.pdm.filevault.service.TyplmContentDataService;
import com.hustcad.plm.pdm.filevault.service.TyplmContentReferenceLinkService;
import com.hustcad.plm.pdm.filevault.service.TyplmEncryptService;
import com.hustcad.plm.pdm.filevault.service.TyplmFileVaultService;
import com.hustcad.plm.pdm.filevault.service.TyplmObjContentLinkService;
import com.hustcad.plm.pdm.filevault.service.TyplmVaultFolderQueryService;
import com.hustcad.plm.pdm.filevault.service.TyplmVaultHostService;
import com.hustcad.plm.pdm.filevault.service.TyplmVaultItemService;
import com.hustcad.plm.pdm.preference.util.TyplmPreferenceValueUtil;
import com.hustcad.plm.pdm.preview.service.TyplmPreviewCommonService;


import com.ty.basic.entity.IdentifierEntity;

import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


@Service
public class TyplmFileHandleServiceImpl implements TyplmFileHandleService {
    private static final Logger log = LoggerFactory.getLogger(TyplmFileHandleServiceImpl.class);
    @Resource
    private TyplmPreviewCommonService typlmPreviewCommonService;
    @Resource
    private TyFileHandleService tyFileHandleService;
    @Resource
    private TyplmContentDataService typlmContentDataService;
    @Resource
    private TyplmObjContentLinkService typlmObjContentLinkService;
    @Resource
    private TyplmContentReferenceLinkService typlmContentReferenceLinkService;
    @Resource
    private TyplmVaultItemService typlmVaultItemService;
    @Resource
    private TyplmFileVaultService typlmFileVaultService;
    @Value("${server.tomcat.basedir}")
    private String tempPath;
    @Resource
    private UploadFileUtils uploadFileUtils;
    @Resource
    private TyplmEncryptService typlmEncryptService;
    @Resource
    private TyplmVaultHostService typlmVaultHostService;
    @Resource
    private TyplmS3ClientAdapterService typlmS3ClientAdapterService;
    @Resource
    private TyplmS3ClientUtil typlmS3ClientUtil;
    @Resource
    private TyplmPreferenceValueUtil typlmPreferenceValueUtil;
    private static final String KEYWORDS = "CopyPreContentDataMarkup";
    private static final String AUTOCAD_CAD = "AutoCAD_CAD";
    @Value("${file.upload.area.type}")
    private String uploadType;
    @Value("${uploadableFileExtensions:.bmp,.jpg,.jpeg,.png,.tif,.gif,.dxf,.svg}")
    private String uploadableFileExtensions;
    @Value("${xlsToXlsx:true}")
    private boolean xlsToXlsx;
    @Resource
    private TyplmVaultFolderQueryService typlmVaultFolderQueryService;
    private static final String FTP_FAIL = "downloadFile from ftp fail.";

    public TyplmFileHandleServiceImpl() {
    }

    public List<FileDataVO> getFileListByEntity(IdentifierEntity entity) {
        entity.setOtype(TyplmEntityBaseUtil.getOtypeByInthid(entity.getOtype()));
        List<FileDataVO> fileListByEntity = this.tyFileHandleService.getFileListByEntity(entity);
        Iterator var3 = fileListByEntity.iterator();

        while (var3.hasNext()) {
            FileDataVO fileDataVO = (FileDataVO) var3.next();
            List<FileDataVO> fileDataVOList = fileDataVO.getFileDataVOList();
            List<String> web = this.typlmPreviewCommonService.queryPreviewFileTypeByEnum("webViewFormat");
            fileDataVOList.forEach((fileData) -> {
                if (web.contains(
                        FileNameUtil.getSuffix(fileData.getFileName()).toLowerCase()) && !"THUMBNAIL".equalsIgnoreCase(
                        fileData.getRolekey())) {
                    fileData.setRolekey("INTEVUE");
                }

            });
        }

        return fileListByEntity;
    }
}